<?php

// autor/i etapy: Matúš Novák
// 18.11.2013

class Model_main extends CI_Model {

    //kontrola spravnych login udajov z DB
    public function can_log_in(){
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('heslo', md5($this->input->post('password')));
        
        $query = $this->db->get('uzivatelia');
        
        if ($query->num_rows == 1) {
            return true;	
        } else{
            return false;
        }
        
    }
    //pridá ešte nepotvrdeného používateľa do temp_users tabulky databazy s jeho potvrdzovacím kľúčom
    // $key je kluc, ktory posluzi na potvrdenie z aktivacneho emailu
    public function add_temp_user($key){
        $data = array(
            'meno' => $this->input->post('meno'),
            'priezvisko' => $this->input->post('priezvisko'),
            'telefon' => $this->input->post('telefon'),
            'skola' => $this->input->post('skola'),
            'email' => $this->input->post('email'),
            'password' => md5($this->input->post('password')),
            'key' => $key
        );    
        $query = $this->db->insert('temp_users',$data);
        if ($query){
            return true;
        } else{
            return false;
        }
    }
    
    //kontrola ci je $key z aktivacneho mailu spravny
    public function is_key_valid($key){
        $this->db->where('key', $key);
        $query = $this->db->get('temp_users');
        
        if ($query->num_rows() == 1){
            return true;
        } else {
            return false;
        }   
    }
    
    //pridá užívateľa do užívateľskej databázy a vymaze ho z temp_users tabulky DB
    public function add_user($key){
        $this->db->where('key', $key);
        $temp_user = $this->db->get('temp_users');
        //vyberie udaje z temp_users a prida ich do users bez key
        if ($temp_user){
            $row = $temp_user->row();
            
            $data = array(
                'meno' => $row->meno,
                'priezvisko' => $row->priezvisko,
                'email' => $row->email,
                'heslo' => $row->password ,
                'telefon' => $row->telefon,
                'skola' => $row->skola,
                'prava' => $row->prava
            );
            
            $did_add_user = $this->db->insert('uzivatelia', $data);
        }
        //ak sa podarilo pridat, vymaze ho z temp_users tabulky a ulozi session
        if ($did_add_user){
            $this->db->where('key', $key);
            $this->db->delete('temp_users');
            
            $data2 = array(
                'meno' => $data['meno'],
                'priezvisko' => $data['priezvisko'],
                'email' => $data['email'],
                'telefon' => $data['telefon'],
                'skola' => $data['skola'],
                'admin' => $data['prava'],
                'is_logged_in' => 1
            );
            $this->session->set_userdata($data2);   
            return true;
        }   return false;
    }
    
    public function get_user_info($temp){
        $this->db->where('email', $temp);
        $user = $this->db->get('uzivatelia');
        if ($user){
            $row = $user->row();
            $data = array(
                'meno' => $row->meno,
                'priezvisko' => $row->priezvisko,
                'email' => $row->email,
                'telefon' => $row->telefon,
                'skola' => $row->skola,
                'admin' => $row->prava,
                'is_logged_in' => 1,
                'id' => $row->id
            );
            return $data;
        }  
        return NULL;   
    } 
    
    //kontrola, podľa email v DB,  či je admin, $temp je kľúč k databáze
    public function is_admin($temp) {
        $this->db->where('email', $temp);
        $user = $this->db->get('uzivatelia');
        if ($user) {
             $row = $user->row();
             $user_check = $row->prava;
             if ($user_check == 1) {
                return true;
             }  else return false;
        }  
        return false;
    }
    
    public function get_id($temp){
        $this->db->where('email', $temp);
        $user = $this->db->get('uzivatelia');
        if ($user) {
             $row = $user->row();
             $user_check = $row->id;
             return $user_check;
        }  
        return NULL;
    }
	
	public function get_text($key){
        $this->db->where('text_name', $key);
        $text = $this->db->get('text_constants');
           if ($text) {
             $row = $text->row();
             $return = $row->text;
             return $return;
        }  
       else { return NULL; }
    }
    
}
